#include "SaveInfo.h"
#include <log/log_err.h>
#include "save/json/SaveJson.h"
#include "base/Intent.h"
#include "save/tinyxml2/SaveTinyXml2.h"

// #define SAVE_JSON//ok
#ifdef SAVE_JSON
static const char *FILENAME = "SaveInfo.json";
#else 
static const char *FILENAME = "SaveInfo.xml";
#endif

static const char *BRIGHTNESS = "Brightness";
static const char *STANDBY_TIME = "StandbyTime";

SaveInfo* SaveInfo::instance = nullptr;

SaveInfo* SaveInfo::getInstance() {
    if (instance == nullptr) {
        instance = new SaveInfo();
    }
    return instance;
}

SaveInfo::SaveInfo(/* args */)
{
#ifdef SAVE_JSON
    saveAbstract = new SaveJson();
#else
    saveAbstract = new SaveTinyXml2();
#endif
}

SaveInfo::~SaveInfo()
{
    if (saveAbstract != nullptr) {
        delete saveAbstract;
    }
}

void SaveInfo::setSaveAbstract(SaveAbstract* saveAbstract)
{
    this->saveAbstract = saveAbstract;
}

void SaveInfo::saveValue(void)
{
    Intent intent;
    
    SIGNWAY_ERROR_GOTO(saveAbstract == nullptr, RETURN, "saveAbstract is null");

    intent.putExtra("name", std::string("胡秋峰"));
    intent.putExtra("age", 22);
    intent.putExtra("fraction", 98.5);
    intent.putExtra("gender", false);
    intent.putExtra("email", std::string("1765155167@qq.com"));

    saveAbstract->saveIntent(FILENAME, intent);

RETURN:
    return;
}

void SaveInfo::loadValue(void)
{
    Intent intent;
    std::string name;
    std::string email;
    int age;
    double fraction;
    bool gender;

    SIGNWAY_ERROR_GOTO(saveAbstract == nullptr, RETURN, "saveAbstract is null");

    saveAbstract->loadIntent(FILENAME, intent);

    intent.getExtraString("name", name);
    intent.getExtraString("email", email);
    intent.getExtraInt("age", age);
    intent.getExtraDouble("fraction", fraction);
    intent.getExtraBool("gender", gender);

    SIGNWAY_LOGW("-----------------info----------------");
    SIGNWAY_LOGW("name      : %s", name.c_str());
    SIGNWAY_LOGW("email     : %s", email.c_str());
    SIGNWAY_LOGW("age       : %d", age);
    SIGNWAY_LOGW("fraction  : %.2f", fraction);
    SIGNWAY_LOGW("gender    : %s", gender ? "女":"男");
    SIGNWAY_LOGW("-------------------------------------");

RETURN:
    return;
}

